package com.hzp.micromall.auth.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @Author houzipeng
 * @Date 2022/2/10 22:00
 * @Version 1.0
 * 安全配置：配置请求访问权限、定义认证管理器、密码加密配置
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//    /**
//     * 自定义用户认证逻辑
//     */
//    @Autowired
//    private UserDetailsService userDetailsService;
//
//    /**
//     * 密码编码器
//     * <p>
//     * 委托方式，根据密码的前缀选择对应的encoder，例如：{bcypt}前缀->标识BCYPT算法加密；{noop}->标识不使用任何加密即明文的方式
//     * 密码判读 DaoAuthenticationProvider#additionalAuthenticationChecks
//     */
//    @Bean
//    public PasswordEncoder passwordEncoder() {
////        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
//        // 强散列哈希实现
//        return new BCryptPasswordEncoder();
//    }
//
//    /**
//     * 认证管理对象
//     * 如果不配置SpringBoot会自动配置一个AuthenticationManager,覆盖掉内存中的用户
//     * @return
//     * @throws Exception
//     */
//    @Bean
//    @Override
//    public AuthenticationManager authenticationManagerBean() throws Exception {
//        return super.authenticationManagerBean();
//    }
//
////    /**
////     * 身份认证接口
////     * @param auth
////     * @throws Exception
////     */
////    @Override
////    protected void configure(AuthenticationManagerBuilder auth) throws Exception
////    {
//////        auth.authenticationProvider(daoAuthenticationProvider());
////        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
////    }
//
//    @Override
//    protected void configure(HttpSecurity http) throws Exception {
//        http
//                // 过滤请求
//                .authorizeRequests()
//                // 对于某些接口允许访问
//                .requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll()
//                .antMatchers("/rsa/getPublicKey",
//                        "/swagger-ui.html",
//                        "/swagger-resources/**",
//                        "/v2/**","/webjars/**").permitAll()
//                // 除上面的所有请求全部需要鉴权认证
//                .anyRequest().authenticated()
//                // CSRF禁用，因为不适用session
//                .and().csrf().disable();
//    }
//
//    @Bean
//    public DaoAuthenticationProvider daoAuthenticationProvider() {
//        DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
//        daoAuthenticationProvider.setUserDetailsService(userDetailsService);
//        daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
//        daoAuthenticationProvider.setHideUserNotFoundExceptions(false);
//        return daoAuthenticationProvider;
//    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll()
                .antMatchers("/rsa/getPublicKey").permitAll()
                .antMatchers("/v2/api-docs").permitAll()
                .anyRequest().authenticated();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

}
